import {
    Component,
    ElementRef,
    EventEmitter,
    Injector,
    Output,
    ViewChild,
} from "@angular/core";
import { AppComponentBase } from "@shared/common/app-component-base";
import {
    ParkGuardServiceProxy,
    CreateParkGuardDto,
    GetParkGuardForEdtiOutput,
    PrakGuardEditDto,
} from "@shared/service-proxies/service-proxies";
import { id } from "@swimlane/ngx-charts/release/utils";
import { ModalDirective } from "ngx-bootstrap";

import { finalize } from "rxjs/operators";

@Component({
    selector: "createOrEditParkingGuradModal",
    templateUrl: "./create-or-edit-parking-guard-modal.component.html",
})
export class CreateOrEditParkingGuardModalComponent extends AppComponentBase {
    @ViewChild("createOrEditModal") modal: ModalDirective;

    @Output() modalSave: EventEmitter<any> = new EventEmitter<any>();
    getParkGuardForEdtiOutput: GetParkGuardForEdtiOutput =
        new GetParkGuardForEdtiOutput();
    prakGuardEdit: PrakGuardEditDto = new PrakGuardEditDto();

    active = false;
    saving = false;
    parkingCoce: string;
    parkingName: string;
    parkingid: number;
    neighbourhood_Name: string;
    neighbourhood_id: number;
    id: number;
    emptyText: "";
    constructor(
        injector: Injector,
        private _parkGuardServiceProxy: ParkGuardServiceProxy
    ) {
        super(injector);
    }

    show(
        parkingCoce: string,
        parkingName: string,
        parkingid: number,
        neighbourhood_Name: string,
        neighbourhood_id: number,
        Id?: number
    ): void {
        const self = this;
        self.active = true;
        self.id = Id;
        self.parkingid = parkingid;
        self.neighbourhood_Name = neighbourhood_Name;
        self.parkingCoce = parkingCoce;
        self.parkingName = parkingName;
        self.neighbourhood_id = neighbourhood_id;
        self._parkGuardServiceProxy
            .getParkingForEdit(Id)
            .subscribe((result) => {
                self.prakGuardEdit = result.prakGuardEdit;
                console.log(self.prakGuardEdit);
            });
        self.modal.show();
    }

    onShown(): void {}

    save(): void {
        const self = this;

        const input = new CreateParkGuardDto();
        (input.neighbourhood_name = this.neighbourhood_Name),
            (input.neighbourhood_id = this.neighbourhood_id),
            (input.guardNo = this.prakGuardEdit.guardNo),
            (input.guradName = this.prakGuardEdit.guradName),
            (input.parkingId = this.parkingid),
            (input.parkingName = this.parkingName),
            (input.parkingCode = this.parkingCoce),
            (this.saving = true);
        console.log(input);
        if (this.id == null) {
            this._parkGuardServiceProxy
                .create(input)
                .pipe(finalize(() => (this.saving = false)))
                .subscribe(() => {
                    this.notify.info(this.l("SavedSuccessfully"));
                    this.close();
                    this.modalSave.emit(null);
                });
        } else {
            input.id = this.prakGuardEdit.id;
            this._parkGuardServiceProxy
                .update(input)
                .pipe(finalize(() => (this.saving = false)))
                .subscribe(() => {
                    this.notify.info(this.l("SavedSuccessfully"));
                    this.close();
                    this.modalSave.emit(null);
                });
        }
    }

    close(): void {
        this.active = false;
        this.modal.hide();
    }
}
